package com.ftg.learn.day10.factory.homework;

import java.io.OutputStreamWriter;
import java.lang.reflect.InvocationTargetException;

/**
 * 类工厂模式
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/8/14 上午10:23
 */
public class BeanFactory {

    public BeanFactory(){}

    public BeanFactory(Class<?>...obj){
        brs = new BeanResult[obj.length];
        for (int i = 0; i < obj.length; i++) {
            BeanResult br = new BeanResult();
            br.setObjName(obj[i]);
            try {
                br.setClassInstance(obj[i].newInstance());
            } catch (InstantiationException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * 通过infos来生成大量实例存储到 BeanResult[] 里面
     *
     * @param infos  类信息
     * @return 返回信息 []
     */
//    public static BeanResult[] createClassInstance(BeanInfo[] infos) {
//        BeanResult[] brs = new BeanResult[infos.length];
//        for (int i = 0; i < brs.length; i++) {
//            BeanResult br = new BeanResult();
//
//            br.setName(infos[i].getName());
//
//            //反射 实例化
//            try {
//                Class<?> c1 = Class.forName(infos[i].getClassPath());
//                Object obj = c1.getConstructor().newInstance();
//                br.setClassInstance(obj);
//            } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException |
//                     InvocationTargetException e) {
//                throw new RuntimeException(e);
//            }
//            brs[i] = br;
//        }
//        return brs;
//    }

    private BeanResult[] brs;

    public void createClassInstance(BeanInfo... infos) {
        brs = new BeanResult[infos.length];
        for (int i = 0; i < brs.length; i++) {
            BeanResult br = new BeanResult();

            br.setName(infos[i].getName());

            //反射 实例化
            try {
                Class<?> c1 = Class.forName(infos[i].getClassPath());
                Object obj = c1.getConstructor().newInstance();
                br.setClassInstance(obj);
            } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException |
                     InvocationTargetException e) {
                throw new RuntimeException(e);
            }
            brs[i] = br;
        }
    }

    public void createClass(BeanInfo... infos) {
        brs = new BeanResult[infos.length];
        for (int i = 0; i < brs.length; i++) {
            BeanResult br = new BeanResult();

            br.setObjName(infos[i].getObjClass());

            //反射 实例化
            try {
                br.setClassInstance(br.getObjName().newInstance());
            } catch (InstantiationException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }
            brs[i] = br;
        }
    }

    public Object getObject(String name) {
        for (BeanResult br : brs) {
            if (br.getName().equals(name)) {
                return br.getClassInstance();
            }
        }
        return null;
    }

    @SuppressWarnings("all")
    public <E> E getObject(Class<E> c, String name) {
        for (BeanResult br : brs) {
            if (br.getName().equals(name)) {
                return (E) br.getClassInstance();
            }
        }
        return null;
    }


    public <E> E getObject(Class<E> c){
        for (BeanResult br : brs) {
            if(br.getObjName().equals(c)){
                Object obj = br.getClassInstance();
                return (E) br.getClassInstance();
            }
        }
        return null;
    }

}
